package resource

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

type Img struct {
	Src string `json:"src"`
}

type ImgResource struct {
	fileurl string
}

func (r ImgResource) Write(str interface{}) {
	imgjson, err := json.Marshal(str)
	if err != nil {
		fmt.Println("json err: ", err)
	} else {
		ioutil.WriteFile(r.fileurl, imgjson, os.ModeAppend)
	}
}

func (u ImgResource) Read() map[string]string {
	jsonfile, _ := ioutil.ReadFile(u.fileurl)
	var imgMap map[string]string
	json.Unmarshal(jsonfile, &imgMap)
	if imgMap == nil {
		return map[string]string{}
	}
	return imgMap
}

func NewImgResource() *ImgResource {
	r := &ImgResource{}
	r.fileurl = "data/img.json"
	return r
}
